package com.ruoyi.service.impl;

import com.ruoyi.domain.AdvMembers;
import com.ruoyi.domain.AdvOrders;
import com.ruoyi.domain.OrderStatisticsDTO;
import com.ruoyi.mapper.AdvOrdersMapper;
import com.ruoyi.mapper.IndexDataMapper;
import com.ruoyi.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexDataMapper indexDataMapper;

    @Autowired
    private AdvOrdersMapper advOrdersMapper;

    @Override
    public int NewMemberToday() {
        return indexDataMapper.NewMemberToday();
    }

    @Override
    public int PayTodayOrder() {
        return indexDataMapper.PayTodayOrder();
    }

    @Override
    public BigDecimal AmountPayableToday() {
        return indexDataMapper.AmountPayableToday();
    }

    @Override
    public int WaitSendGoods() {
        return indexDataMapper.WaitSendGoods();
    }

    @Override
    public int WaitRefund() {
        return indexDataMapper.WaitRefund();
    }

    @Override
    public int WaitSalesReturn() {
        return indexDataMapper.WaitSalesReturn();
    }

    @Override
    public List<AdvMembers> SalesRank() {
        return indexDataMapper.SalesRank();
    }

    @Override
    public int GoodsSum() {
        return indexDataMapper.GoodsSum();
    }

    @Override
    public int SalesMemberSum() {
        return indexDataMapper.SalesMemberSum();
    }

    @Override
    public int OrderSumByLastWeek() {
        return indexDataMapper.OrderSumByLastWeek();
    }

    @Override
    public BigDecimal WeeklySales() {
        return indexDataMapper.WeeklySales();
    }

    @Override
    public BigDecimal RefundAmountByLastWeek() {
        return indexDataMapper.RefundAmountByLastWeek();
    }

    @Override
    public Map<String, Object> getAllData() {
        Map<String, Object> data = new HashMap<>();
        data.put("newMemberToday", indexDataMapper.NewMemberToday());//今日新增会员
        data.put("payTodayOrder", indexDataMapper.PayTodayOrder());//今日付款订单
        BigDecimal amountPayableToday  = indexDataMapper.AmountPayableToday();
        data.put("amountPayableToday",amountPayableToday != null ? amountPayableToday : BigDecimal.ZERO );//今日付款金额
        data.put("waitSendGoods", indexDataMapper.WaitSendGoods());//待处理发货
        data.put("waitRefund", indexDataMapper.WaitRefund());//待处理退款
        data.put("waitSalesReturn", indexDataMapper.WaitSalesReturn());//待处理退货
        data.put("salesRank", indexDataMapper.SalesRank());  //List
        data.put("goodsSum", indexDataMapper.GoodsSum());
        data.put("salesMemberSum", indexDataMapper.SalesMemberSum());
        data.put("members", indexDataMapper.MemberUser());
        data.put("orderSumByLastWeek", indexDataMapper.OrderSumByLastWeek());
        BigDecimal weeklySales = indexDataMapper.WeeklySales();
        data.put("weeklySales", weeklySales != null ? weeklySales : BigDecimal.ZERO );
        BigDecimal refundAmount = indexDataMapper.RefundAmountByLastWeek();
        data.put("refundAmountByLastWeek",refundAmount != null ? refundAmount : BigDecimal.ZERO  );
        List<OrderStatisticsDTO> ordersOfLastWeek = advOrdersMapper.findOrdersOfLastWeek();
        if (ordersOfLastWeek.isEmpty()) {
            // 获取当前日期
            LocalDate today = LocalDate.now();

            // 循环生成最近一周的日期
            for (int i = 6; i >= 0; i--) {
                LocalDate date = today.minusDays(i);
                // 创建 OrderStatisticsDTO 对象并设置金额为 0
                OrderStatisticsDTO dto = new OrderStatisticsDTO(date.toString(), 0.0);
                ordersOfLastWeek.add(dto);
            }
        }
        data.put("ordersOfLastWeek", ordersOfLastWeek); // 将生成的数据放入返回结果中
        return data;
    }

    @Override
    public List<OrderStatisticsDTO> findOrdersOfLastWeek() {
        return advOrdersMapper.findOrdersOfLastWeek();
    }
}
